import pymongo
import datetime
import struct
import time

mongo_client = pymongo.MongoClient('127.0.0.1', 27017)
# print(mongo_client.server_info())  # 判断是否连接成功
mongo_db = mongo_client['picsad']
mongo_collection = mongo_db['point_cloud']

file_name = "/data/kitti_l.bin"
f = open(file_name, "rb")

points = []
while True:
    x_str = f.read(4)
    if x_str == b'':
        break
    y_str = f.read(4)
    z_str = f.read(4)
    x = struct.unpack('f', x_str)[0]
    y = struct.unpack('f', y_str)[0]
    z = struct.unpack('f', z_str)[0]
    points.append({
        'x': x,
        'y': y,
        'z': z
    })

    if len(points) > 64000:
        start = time.clock()
        mongo_collection.insert_many(points)
        end = time.clock()
        print(end - start)
        points.clear()

f.close()
